package com.itheima04;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class Test04 {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\阳光正直夏明伟15805655221\\Desktop\\测试用");
        ArrayList<File> dirlist = new ArrayList<>();
        ArrayList<File> files = new ArrayList<>();

        diGui(file, dirlist, files);
        HashMap<String, Integer> mp = new HashMap<>();
        for (int i = 0; i < files.size(); i++) {
            File f = files.get(i);
            String[] split = f.getPath().split("\\.");
            if (split.length > 1) {
                String key = split[split.length - 1];
                if (mp.containsKey(key)) {
                    Integer integer = mp.get(key);
                    integer++;
                    mp.put(key, integer);
                } else {
                    mp.put(key, 1);
                }
            }
        }
        Set<Map.Entry<String, Integer>> entries = mp.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + "文件有" + value + "个");
        }

    }

    public static void diGui(File file, ArrayList<File> dirlist, ArrayList<File> files) {
        if (file.isFile()) {
            files.add(file);
        }
        if (file.isDirectory()) {
            dirlist.add(file);
            File[] files1 = file.listFiles();
            if (files1 != null) {
                for (File chlid : files1) {
                    diGui(chlid, dirlist, files);
                }
            }
        }
    }
}
